{% extends "base.html" %}

{% block title %}工作负载 | 详情{% endblock %}

{% block head %}
{{ super() }}
<!-- DataTables -->
<link rel="stylesheet" href="/static/bower_components/datatables.net-bs/css/dataTables.bootstrap.min.css">
{% endblock %}


{% block content %}
<!-- 页面头 -->
<section class="content-header">
  <!--头部标题-->
  <h1>
    POD
    <!--    <small>advanced tables</small>-->
  </h1>
  <!--面包屑-->
  <ol class="breadcrumb">
    <li><a href="/"><i class="fa fa-home"></i> 主页</a></li>
    <li><a href="/blues/blues/clusterler/cluster">集群管理</a></li>
    <li>工作负载</li>
    <li class="active">详请</li>
  </ol>
</section>


<!-- 页面内容 -->
<section class="content">
  <div class="row">
    <div class="col-xs-12">
<!--      <div style="margin-bottom: 10px">-->
<!--        命名空间：<span id="namespace"></span>&nbsp;&nbsp;-->
<!--        类型：<span id="type"></span>&nbsp;&nbsp;-->
<!--        主镜像：<span id="image"></span>&nbsp;&nbsp;-->
<!--      </div>-->

            <!--    顶部工具栏-->
      <form class="form-inline" style="margin-bottom: 10px">
        <div class="form-group">
          <label for="selectCluster"> <span style="color: red">*</span> 集群</label>
          <select class="form-control need-refresh-table" id="selectCluster" name="cluster_id" required>
            <option value="" selected>--请选择--</option>
          </select>
        </div>
        &nbsp;
        <div class="form-group">
          <label for="selectNamespace"> <span style="color: red">*</span> 命名空间</label>
          <select class="form-control need-refresh-table" id="selectNamespace" name="namespace" required>
            <!--            <option value="_all" selected>ALL</option>-->
          </select>
        </div>
        &nbsp;
      </form>

      <!--表格1-->
      <div class="box">
        <div class="box-header">
          <!--                  <h3 class="box-title">节点概览</h3>-->

        </div>
        <div class="box-body">
          <table id="example1" class="table table-bordered table-hover">
            <thead>
            <tr>
              <th>Pod名称</th>
              <th>命名空间</th>
              <th>镜像</th>
              <th>主机</th>
              <th>状态</th>
              <th>原因</th>
              <th>重启次数</th>
              <th>创建时间</th>
              <th>操作</th>
            </tr>
            </thead>
            <tbody id="tbody">
            </tbody>
          </table>
        </div>
        <!-- /.box-body -->
                <!-- Loading (remove the following to stop the loading)-->
        <div id="loading" class="overlay" style="display: block">
          <i class="fa fa-refresh fa-spin"></i>
        </div>
        <!-- end loading -->
      </div>
      <!-- /.box -->

    </div>
    <!-- /.col -->
  </div>
  <!-- /.row -->
</section>
<!-- /.页面内容 -->
{% endblock %}


{% block script %}

<script type="text/javascript">
    // 定义全局变量
    var type = 'pods'
    // 解析URL
    var pathName = window.document.location.pathname;
    var pathList = pathName.split("/")
    var cluster_id = pathList[2]
    var namespace = pathList[3]
    var type = pathList[4]
    var selector = "{{selector}}"
    var resource = pathList[5]
    console.log(pathList)
    console.log(resource)
    var main_image = ''
    $('#title').text(resource)
    $('#namespace').text(namespace)
    $('#type').text(type)


    // 页面载入完毕后执行接口调用
    $(document).ready(function () {
        let url = '/api/v1/cluster/' + cluster_id + '/' + namespace + '/pods/?selector=' + selector
        $.get(url, function (result) {
            console.log(result)
            // 显示主镜像名
            main_image = result.items[0].spec.containers[0].image
            $('#image').text(main_image)


            // 填充Pod列表
            for (let i in result.items) {
                let pod = result.items[i].metadata.name
                let ns = result.items[i].metadata.namespace
                let container = result.items[i].spec.containers[0].name
                let href = '/cluster/' + cluster_id + '/' + namespace + '/containers/' + pod
                let text = '<tr>'
                text += '<td>' + pod + '</td>'
                text += '<td>' + ns + '</td>'

                // text += '<td>' + container + '</td>'
                text += '<td>'
                // console.log(result.items[i].spec.containers.length)
                for (let j = 0; j < result.items[i].spec.containers.length; j++) {
                    if (j == 0) {
                        text += result.items[i].spec.containers[j].image
                    } else {
                        text += '<br>' + result.items[i].spec.containers[j].image
                    }
                }
                text += '</td>'
                text += '<td>' + result.items[i].spec.nodeName + '<br>' + result.items[i].status.hostIP + '</td>'
                let status = ''
                let reason = ''
                for (let x in result.items[i].status.containerStatuses[0].state) {
                    status += x
                    if (result.items[i].status.containerStatuses[0].state[x].reason) {
                        reason = result.items[i].status.containerStatuses[0].state[x].reason
                    }

                }

                text += '<td>' + status + '</td>'
                text += '<td>' + reason + '</td>'
                // text += '<td>' + JSON.stringify(result.items[i].metadata.labels) + '</td>'
                let restart_num = 0
                for (let k in result.items[i].status.containerStatuses) {
                    // console.log(result.items[i].status.containerStatuses[k].restartCount)
                    restart_num += Number(result.items[i].status.containerStatuses[k].restartCount)
                }
                text += '<td>' + restart_num + '</td>'
                text += '<td>' + result.items[i].metadata.creationTimestamp + '</td>'

                let ws_url = '/cluster/logs/' + cluster_id + '/' + namespace + '/' + pod + '/' + container
                let exec_url = '/cluster/terminal/' + cluster_id + '/' + namespace + '/' + pod + '/' + container

                text += '<td><a href="' + ws_url + '">日志 </a>'
                text += '<a href="' + exec_url + '">命令行 </a>'
                text += '<a href="' + href + '">容器</a>'
                text += '</td></tr>'

                $('#tbody').append(text)
            }
        })

        // 获取并显示副本数
        // refreshReplicas()
    })


    function refreshReplicas() {
        let url = '/api/v1/cluster/' + cluster_id + '/' + namespace + '/deployments/' + resource + '/scale'
        $.get(url, function (data) {
            console.log(data)
            let replicas = data.spec.replicas
            if (replicas) {
                $('#replicas').text(replicas)
            } else {
                $('#replicas').text(0)
            }

            console.log(replicas)

        })
    }


    $('#btn_restart').click(function () {
        let restart_url = '/api/v1/cluster/' + cluster_id + '/' + namespace + '/' + type + '/' + resource + '/restart'
        $.get(restart_url, function (data) {
            console.log(data)
            if (data == 'success') {
                bootbox.alert('重新部署成功', function () {
                    setTimeout(function () {
                        location.reload()
                    }, 250)
                });
            } else {
                bootbox.alert('重新部署失败')
            }
        })
    })



    $('#btn_update').click(function () {
        bootbox.prompt({
            size: "large",
            title: "请输入新的镜像",
            value: main_image,
            callback: function (result) {
                /* result = String containing user input if OK clicked or null if Cancel clicked */
                if (result) {
                    console.log('update image:' + result.trim())
                    url = '/api/v1/cluster/' + cluster_id + '/' + namespace + '/' + type + '/' + resource + '/update/?image=' + result.trim()
                    $.get(url, function (data) {
                        console.log(data)
                        setTimeout(function () {
                            location.reload()
                        }, 250)
                    })
                }
            }
        });
    })

</script>

{% endblock %}